package servlet.rool;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import pojo.Admin;
import service.AdminService;
import service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员表导出到Excel的servlet
 */
@WebServlet(value = "/AdminExportServlet")
public class AdminExportServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        request.setCharacterEncoding("UTF-8");
        String fids = request.getParameter("ids");
        if (null != fids && !("".equals(fids))){
            String[] fid = fids.split(",");
            List<Admin> list=new ArrayList<Admin>();
            AdminService as=new AdminServiceImpl();
            for (String id : fid) {
                list.add(as.queryAdmin(Integer.parseInt(id)));
            }
            String[] title = {"管理员名称", "管理员账号", "管理员性别", "手机号", "邮箱"};
            //创建Excel工作簿
            HSSFWorkbook workbook=new HSSFWorkbook();
            //创建一个工作表
            HSSFSheet sheet = workbook.createSheet();
            //创建第一行  sheet.createRow(0)表示第一行
            HSSFRow row=sheet.createRow(0);
            //列
            HSSFCell cell = null;
            for (int i = 0; i < title.length; i++) {
                //循环往第一行列中进行赋值
                cell=row.createCell(i);
                cell.setCellValue(title[i]);
            }
            //存储参数
            //循环创建其他行并对每行的列增加数据
            for(int i=0;i<list.size();i++){
                HSSFRow nextrow=sheet.createRow(i+1);
                HSSFCell dataCell=nextrow.createCell(0);
                dataCell.setCellValue(list.get(i).getAdName());
                dataCell=nextrow.createCell(1);
                dataCell.setCellValue(list.get(i).getAdAccount());
                dataCell=nextrow.createCell(2);
                dataCell.setCellValue(list.get(i).getAdSex()==1 ? "男" : "女");
                dataCell=nextrow.createCell(3);
                dataCell.setCellValue(list.get(i).getAdPhone());
                dataCell=nextrow.createCell(4);
                dataCell.setCellValue(list.get(i).getAdEmail());
            }
            OutputStream output = response.getOutputStream();
            response.reset();
            //导入路径,选择
            response.setHeader("Content-disposition", "attachment; filename=admins.xls");
            response.setContentType("application/msexcel");
            workbook.write(output);
            output.close();
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
